package org.example.service;

import javafx.application.Platform;
import org.example.controller.JavaProcessManagementController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat;
import java.util.concurrent.Executors;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author: wwy
 * @Date: 2020/12/1 15:36
 */
@Service
public class PanelMainService {
    @Autowired
    private JavaProcessManagementController javaProcessManagementController;
    /**
     * app运行时间
     */
    public void appRunTime(){
        JavaProcessManagementController.executorService.execute(()->{
            //计算时间
            int day = 0;
            int h = 0;
            int m = 0;
            int s = 0;
            while (true){
                try {
                    s++;
                    if(s == 60){
                        m++;
                        s = 0;
                    }
                    if(m == 60){
                        h++;
                        m=0;
                    }
                    String s1 = this.timeFormat(h, m, s);
                    Platform.runLater(()->{
                        javaProcessManagementController.showTimerLabel(s1);
                    });

                    //渲染ui界面
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private String timeFormat(int h,int m,int s){
        String timeStr = "";
        int hLength = String.valueOf(h).length();
        int mLength = String.valueOf(m).length();
        int sLength = String.valueOf(s).length();
        if(hLength <=1){
            timeStr = timeStr+"0"+h+":";
        }else{
            timeStr = timeStr+h+":";
        }
        if(mLength <=1){
            timeStr = timeStr+"0"+m+":";
        }else{
            timeStr = timeStr+m+":";
        }
        if(sLength <= 1){
            timeStr = timeStr+"0"+s;
        }else{
            timeStr = timeStr+s;
        }
        return timeStr;
    }
}
